package com.ishow.noah.modules.main

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.view.View
import com.ishow.common.extensions.parseJSON
import com.ishow.common.extensions.toast
import com.ishow.common.widget.edittext.EditTextPro
import com.ishow.noah.R
import com.ishow.noah.entries.App
import com.ishow.noah.modules.base.AppBaseActivity
import com.ishow.noah.utils.SignatureUtils
import kotlinx.android.synthetic.main.activity_app_detail.*

/**
 * Created by yuhaiyang on 2017/6/11.
 * App详情
 */
class AppDetailActivity : AppBaseActivity(), View.OnClickListener {
    private lateinit var app: App
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_app_detail)
    }

    override fun initNecessaryData() {
        super.initNecessaryData()
        val intent = intent

        intent?.run {
            val detail = getStringExtra(App.Key.DETAIL)!!
            app = detail.parseJSON()
            getAppDetail()
        }
    }

    override fun initViews() {
        super.initViews()
        topBar.setText(app.title)
        appPackage.text = app.packageName
        version.text = app.version
        icon.setImageDrawable(app.icon)

        val signature = SignatureUtils.getSignature(this, app.packageName)
        sign.setRightTextClickListener(this)
        sign.setInputText(signature)
        submit.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.sign -> copy2ClipBoard()
            R.id.submit -> logPrint()
            else -> {
                val parent = v.parent
                if (parent != null && parent is EditTextPro) {
                    onClick(parent as View)
                }
            }
        }

    }

    private fun copy2ClipBoard() {
        val builder = StringBuilder()
            .append(app.packageName)
            .append("\n")
            .append(sign.inputText)


        val clip = ClipData.newPlainText("sign", builder.toString())
        val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        cm.setPrimaryClip(clip)
        toast(R.string.sing_copy_success)
    }

    private fun logPrint() {
        val builder = StringBuilder()
        builder.append("\n***************  ")
        builder.append(app.title)
        builder.append("的包名")
        builder.append("  ***************\n")
        builder.append(app.packageName)
        builder.append("\n")
        builder.append(app.launchClass)
        builder.append("\n***************  ")
        builder.append(app.title)
        builder.append("的签名")
        builder.append("  ***************\n")
        builder.append(sign.inputText)
        val result = builder.toString()
        Log.i(TAG, result)

        toast("Log已经输出，TAG为：SIGN")
    }

    private fun getAppDetail() {
        val pm: PackageManager = context.packageManager
        app.icon = pm.getApplicationIcon(app.packageName)
    }

    companion object {
        private const val TAG = "SIGN"
    }
}